package com.dstz.bpm.plugin.global.nodemessage.executer;

import cn.hutool.core.collection.CollectionUtil;
import com.dstz.base.core.util.StringUtil;
import com.dstz.bpm.api.engine.action.cmd.BaseActionCmd;
import com.dstz.bpm.api.engine.context.BpmContext;
import com.dstz.bpm.api.engine.plugin.def.UserAssignRule;
import com.dstz.bpm.engine.plugin.factory.BpmPluginSessionFactory;
import com.dstz.bpm.engine.plugin.runtime.abstact.AbstractBpmExecutionPlugin;
import com.dstz.bpm.engine.plugin.session.BpmExecutionPluginSession;
import com.dstz.bpm.engine.plugin.session.BpmUserCalcPluginSession;
import com.dstz.bpm.plugin.global.nodemessage.def.NodeMessage;
import com.dstz.bpm.plugin.global.nodemessage.def.NodeMessagePluginDef;
import com.dstz.bpm.plugin.node.userassign.executer.UserAssignRuleCalc;
import com.dstz.sys.api.freemark.IFreemarkerEngine;
import com.dstz.sys.api.groovy.IGroovyScriptEngine;
import com.dstz.sys.api.jms.model.DefaultJmsDTO;
import com.dstz.sys.api.jms.model.JmsDTO;
import com.dstz.sys.api.jms.model.msg.NotifyMessage;
import com.dstz.sys.api.jms.producer.JmsProducer;
import com.dstz.sys.api.model.SysIdentity;
import com.dstz.sys.util.ContextUtil;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class NodeMessagePluginExecutor extends AbstractBpmExecutionPlugin<BpmExecutionPluginSession, NodeMessagePluginDef> {
   @Resource
   IGroovyScriptEngine m;
   @Resource
   JmsProducer n;
   @Autowired
   IFreemarkerEngine o;

   @Override
   public Void execute(BpmExecutionPluginSession pluginSession, NodeMessagePluginDef pluginDef) {
      List<NodeMessage> messages = pluginDef.getNodeMessageList();
      Iterator var4 = messages.iterator();

      while(var4.hasNext()) {
         NodeMessage nodeMessage = (NodeMessage)var4.next();
         if (this.a(pluginSession, nodeMessage)) {
            List<JmsDTO> JmsDto = this.a(nodeMessage, pluginSession);
            this.n.sendToQueue(JmsDto);
            this.LOG.debug("【节点消息插件】发送消息成功！时机：{}，消息标题：{}", pluginSession.getEventType().getValue(), nodeMessage.getDesc());
         }
      }

      return null;
   }

   private List<JmsDTO> a(NodeMessage nodeMessage, BpmExecutionPluginSession session) {
      String[] msgType = nodeMessage.getMsgType().split(",");
      String htmlTemplate = nodeMessage.getHtmlTemplate();
      String textTemplate = nodeMessage.getTextTemplate();

      try {
         if (StringUtil.isNotEmpty(htmlTemplate)) {
            htmlTemplate = htmlTemplate.replaceAll("&lt;", "<").replaceAll("&gt;", ">");
            htmlTemplate = this.o.parseByString(htmlTemplate, session);
         }

         if (StringUtil.isNotEmpty(textTemplate)) {
            textTemplate = this.o.parseByString(textTemplate, session);
         }
      } catch (Exception var13) {
         this.LOG.error("htmlTemplate:{};textTempalte:{}", htmlTemplate, textTemplate);
         this.LOG.error("instId[{}]消息发送插件解析消息模板失败，可能原因为:{}", new Object[]{session.getBpmInstance().getId(), var13.getMessage(), var13});
         var13.printStackTrace();
      }

      List userList;
      if (CollectionUtil.isEmpty(nodeMessage.getUserRules())) {
         BaseActionCmd cmd = (BaseActionCmd)BpmContext.getActionModel();
         userList = cmd.getBpmIdentity(cmd.getNodeId());
      } else {
         userList = this.a(session, nodeMessage.getUserRules());
      }

      if (CollectionUtil.isEmpty(userList)) {
         this.LOG.debug("【节点消息插件】没有需要发送的消息！原因：接收消息人员为空。 节点：{}，时机：{}，消息标题：{}", new Object[]{this.getActivitiId(session), session.getEventType().getValue(), nodeMessage.getDesc()});
         return Collections.emptyList();
      } else {
         List<JmsDTO> jmsDto = new ArrayList();
         String[] var8 = msgType;
         int var9 = msgType.length;

         for(int var10 = 0; var10 < var9; ++var10) {
            String type = var8[var10];
            NotifyMessage message = new NotifyMessage(nodeMessage.getDesc(), htmlTemplate, ContextUtil.getCurrentUser(), userList);
            message.setTextContent(textTemplate);
            jmsDto.add(new DefaultJmsDTO(type, message));
         }

         return jmsDto;
      }
   }

   private List<SysIdentity> a(BpmExecutionPluginSession pluginSession, List<UserAssignRule> ruleList) {
      BpmUserCalcPluginSession calcSession = BpmPluginSessionFactory.buildBpmUserCalcPluginSession(pluginSession);
      return UserAssignRuleCalc.a(calcSession, ruleList, false);
   }

   private boolean a(BpmExecutionPluginSession session, NodeMessage nodeMessage) {
      if (StringUtil.isNotEmpty(nodeMessage.getEvent()) && !nodeMessage.getEvent().equals(session.getEventType().getKey())) {
         return false;
      } else if (StringUtil.isNotEmpty(nodeMessage.getNodeId()) && !nodeMessage.getNodeId().equals(this.getActivitiId(session))) {
         return false;
      } else {
         if (StringUtil.isNotEmpty(nodeMessage.getCondition())) {
            Boolean support = this.m.executeBoolean(nodeMessage.getCondition(), session);
            if (!support) {
               return false;
            }
         }

         return true;
      }
   }
}
